import axios from 'axios';
import config from '../../config/config.js'
import $F from '../common/func.js'
import Qs from 'qs'

export default {
	async request(options) {
		if(options.caipiao){
			options.baseURL = config.baseURL2
		}else{
			options.baseURL = $F.getApiUrl()
		}
		// 
	
	
		if(!options.baseURL){
			return false;
		}
		//后加的
		// if(localStorage.getItem('member_sessionid')&&localStorage.getItem('member_auth_id')){
		// 	console.log('缓存有member_sessionid',localStorage.getItem('member_sessionid'))
		// 	options.data.member_sessionid=localStorage.getItem('member_sessionid')
		// 	options.data.member_auth_id=localStorage.getItem('member_auth_id')
		// }
		if (options.method.toUpperCase() === 'GET') {
			options.params = options.data
		} else {
	
			options.data = Qs.stringify(options.data)
		}
	
		options.headers = options.headers !== undefined ? options.headers : {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
		options.headers.token = localStorage.getItem('token');
		options.headers.lang = localStorage.getItem('locale');
		options.method = options.method !== undefined ? options.method : 'GET'
		console.log('获取到的token',localStorage.getItem('token'))

		return new Promise((resolve, reject) => {
			axios(options)
				.then((response) => {
					let data = response.data;
					//201账号未登录
					if (data.code == 201) {
						console.log('是过期了吗')
						localStorage.setItem('login',1)
						localStorage.removeItem('token');
						localStorage.removeItem('userInfo');
						console.log(localStorage.getItem('login'),'这里能获取到吗')
						if(localStorage.getItem('login') != 1 || window.location.hash != '#/'){
							window.location.replace(window.location.pathname)
						}
						
					}else if(data.code == 202 && window.location.hash !='#/index/maintenance'){
						console.log('系统维护',window.location.hash)
						console.log(window.location.pathname,'window.location.pathname')
						window.location.replace('/#/index/maintenance')
						// this.$router.push('/index/maintenance')
					}

					resolve(data);
				}).catch((error) => {
					console.log("接口请求异常：", error);
					resolve(error);
				});
		});
	},
	async getImg(url) {
		let uu = url
		if (url.indexOf('://') == -1) {
			uu = config.imgDomain + url
		}
		return new Promise((resolve, reject) => {
			axios({
				method: 'get',
				url: uu,
			}).then(function(response) {
				resolve(response.data);
			}).catch(function(error) {
				console.log(error)
			})
		});
	},

	//图片上传
	async upload(data) {
		return new Promise((resolve, reject) => {
			let token = localStorage.getItem("token");

			let imgObj = {
				percent: 0,
				imgUrl: "",
				code: ""
			}

			axios({
				method: 'post',
				headers: {
					"Content-Type": "multipart/form-data",
					token
				},
				url: $F.getApiUrl() + '/admin/Upload/addFiles', //自定义上传url
				data,
				onUploadProgress: progressEvent => {
					let percent = (progressEvent.loaded / progressEvent.total * 100) | 0
					// console.log("--------",progressEvent);
					imgObj.percent = percent;

					resolve(imgObj);
				}
			}).then((res) => {
				imgObj.code = res.data.code;

				if (res.data.code == 0) {
					let imgUrl = $F.getApiUrl() + res.data.data.url;

					imgObj.imgUrl = imgUrl;

					resolve(imgObj);
				}
			}).catch((err) => {
				// console.log(err);
				reject(err);
			})

		});
	},

	//文件上传
	async uploadFile(data) {
		return new Promise((resolve, reject) => {
			let token = localStorage.getItem("token");
			axios({
				method: 'post',
				headers: {
					"Content-Type": "multipart/form-data",
					token
				},
				url: $F.getApiUrl() + '/api/user/upload', //自定义上传url
				data
			}).then((res) => {
				if (res.data.code == 0) {
					// console.log("res.data",res.data);
					var data = res.data;
					resolve(data);
				} else {
					reject(res);
				}
			}).catch((err) => {
				// console.log(err);
				reject(err);
			})

		});
	}

}